//
//  KSReviewPassViewController.swift
//  Supermarket
//
//  Created by liuxm on 2017/9/25.
//  Copyright © 2017年 kushao. All rights reserved.
//

import UIKit

/// 发货物流
class KSReviewPassViewController: KSBaseViewController,KSChoosePressageDelegate {
    
    @IBOutlet weak var submitBtn: UIButton!
    
    @IBOutlet weak var logisticsCodeTxt: UITextField!
    private var choosePressageView = KSChoosePressageView()
    var orderId = ""
    private var pressageIdArray: [Int] = []
    private var pressageNameArray: [String] = []
    private var select: NSInteger = 0
    @IBOutlet weak var pressageBtn: UIButton!
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view.
        self.title = "发货物流"
        
        self.submitBtn.setBackgroundImage(KSTools.createImageWithColor(color: KSColor.kColorMainRed), for: UIControlState.normal)
        self.submitBtn.setBackgroundImage(KSTools.createImageWithColor(color: KSColor.kColorButtonHighlight), for: UIControlState.highlighted)
        self.submitBtn.setBackgroundImage(KSTools.createImageWithColor(color: KSColor.kColorButtonDisabled), for: UIControlState.disabled)
        
        self.pressageBtn.setTitleColor(UIColor.black, for: .selected)
        self.pressageBtn.setTitleColor(UIColor.colorWithHexString(hex: "CECED3"), for: .normal)
        
        let rightBtn = UIButton.init(frame: CGRect.init(x: 0, y: 40, width: 50, height: 50))
        rightBtn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        rightBtn.setTitle("跳过", for: .normal)
        rightBtn.setTitleColor(UIColor.red, for: .normal)
        rightBtn.addTarget(self, action: #selector(jumpOverEvent(button:)), for: .touchDown)
        let rightBar = UIBarButtonItem.init(customView: rightBtn)
        self.navigationItem.rightBarButtonItem = rightBar
        
        self.requestPressageData()
    }
    
    private func requestPressageData() {
        self.request.requestIosKuaidiList()
    }
    
    private func requestAudiSend() {
        let parameters: [String : Any] = ["order_id" : orderId,"kuaidi_id" : pressageIdArray[select],"kuaidi_code" : logisticsCodeTxt.text!]
        self.request.requestIosAudiSend(parameters: parameters)
    }
    
    override func requestSuccess(result: String, apiType: KSApiType) {
        super.requestSuccess(result: result, apiType: apiType)
        let model = KSKuaidiListModel.deserialize(from: result)
        if (model?.data?.count)! > 0 {
            for i in 0...(model?.data?.count)! - 1 {
                let dataModel = model?.data![i]
                pressageIdArray.append((dataModel?.kuaidi_id)!)
                pressageNameArray.append((dataModel?.name)!)
            }
        }
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func choosePressage(row: NSInteger) {
        self.select = row
        self.pressageBtn.isSelected = true
        self.pressageBtn.setTitle(pressageNameArray[row], for: .normal)
    }
    
    @objc func jumpOverEvent(button: UIButton) {
        let parameters: [String : Any] = ["order_ids" : [orderId],"operate_state" : "2"]
        self.request.requestIosOrderAuditing(parameters: parameters)
    }
    
    @IBAction func chooseExpressageEvent(_ sender: Any) {
        if choosePressageView.pressageArray.count > 0 {
            choosePressageView.isHidden = false
        } else {
            choosePressageView.delegate = self
            choosePressageView.pressageArray = pressageNameArray
            self.view.addSubview(choosePressageView)
            choosePressageView.snp.makeConstraints { (make) in
                make.edges.equalTo(UIEdgeInsetsMake(0, 0, 0, 0))
            }
        }
        
    }
    
    @IBAction func logisticsCodeChange(_ sender: Any) {
        self.judgetextFieldisEmpty()
    }
    
    private func judgetextFieldisEmpty() {
        if (self.logisticsCodeTxt.text?.isEmpty)! {
            self.submitBtn.isEnabled = false
        } else {
            self.submitBtn.isEnabled = true
        }
    }
    
    @IBAction func submitBtnClick(_ sender: Any) {
        self.requestAudiSend()
    }
    
}
